package com.wnxy.sdd.front.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.hash.BloomFilter;
import com.wnxy.sdd.front.entity.Course;
import com.wnxy.sdd.front.mapper.CourseMapper;
import com.wnxy.sdd.front.service.ICourseService;
import com.wnxy.sdd.front.vo.CourseVo;
import com.wnxy.sdd.front.vo.IndexCourseQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {
//    @Override
//    public List<IndexCourseQueryVo> queryNewCourse(int num) {
//        List<IndexCourseQueryVo> list = baseMapper.queryNewCourse(num);
//        return list;
//    }

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private BloomFilter<String> bloomFilter;//引入布隆过滤器

    @Override
    public List<IndexCourseQueryVo> queryNewCourse(int num) {

        // 生成Redis缓存的key
        String cacheKey = "newCourse:" + num;
        //替换一个方案：BloomFilter过滤器帮我们判断
        if(!bloomFilter.mightContain(cacheKey)){
            System.out.println("key不存在");
            return null;
        }
        // 尝试从Redis缓存中获取数据
        List cachedResult = redisTemplate.opsForList().range(cacheKey, 0, -1);
        return cachedResult;
    }


    public List<IndexCourseQueryVo> queryPage(int current, int size, String subjectId) {
        IPage<IndexCourseQueryVo> iPage = new Page<>(current, size);
        IPage<IndexCourseQueryVo> courseQueryVoIPage = baseMapper.selectCoursePage(iPage, subjectId);
        return courseQueryVoIPage.getRecords();
    }

    @Override
    public CourseVo queryCourseVoById(String id) {
        return baseMapper.queryCourseVoById(id);
    }

    @Override
    public List<CourseVo> findCourseByBuyCount(int i) {
        return baseMapper.findCourseByBuyCount(i);
    }
}
